/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header$
 * $Id$
 * $Author$
 * $Date$
 * $Revision$
 */
package com.elancom.framework.commons.asm;

import java.io.IOException;
import java.io.PrintWriter;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.util.TraceClassVisitor;

/**
 * @author elancom
 * @date 2012-7-28
 */
public class ClassPrinter implements ClassVisitor {

	/*
	 * (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visit(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
	 */
	@Override
	public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
		System.out.println(name + " extends " + superName);
	}

	/*
	 * (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitAnnotation(java.lang.String, boolean)
	 */
	@Override
	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitAttribute(org.objectweb.asm.Attribute)
	 */
	@Override
	public void visitAttribute(Attribute attr) {
		System.out.println("attr:" + attr);
	}

	/*
	 * (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitEnd()
	 */
	@Override
	public void visitEnd() {
		System.out.println("}");
	}

	/*
	 * (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitField(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object)
	 */
	@Override
	public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
		System.out.println(" " + desc + " " + name + "{");
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitInnerClass(java.lang.String, java.lang.String, java.lang.String, int)
	 */
	@Override
	public void visitInnerClass(String name, String outerName, String innerName, int access) {
	}

	/*
	 * (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitMethod(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
	 */
	@Override
	public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exception) {
		System.out.println(name + desc);
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitOuterClass(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void visitOuterClass(String arg0, String arg1, String arg2) {
	}

	/*
	 * (non-Javadoc)
	 * @see org.objectweb.asm.ClassVisitor#visitSource(java.lang.String, java.lang.String)
	 */
	@Override
	public void visitSource(String source, String debug) {
		// System.out.println("source:" + source + ", debug:" + debug);
	}

	public static void main(String[] args) throws IOException {
		// ClassPrinter classPrinter = new ClassPrinter();
		ClassVisitor cv = new TraceClassVisitor(new PrintWriter(System.out));
		ClassReader classReader = new ClassReader("com.elancom.framework.commons.asm.ClassPrinter");
		classReader.accept(cv, 0);
	}
}
